/**
 * 订单列表打印模板工具类
 */
import { hiprint, defaultElementTypeProvider } from 'vue-plugin-hiprint'

class OrderListPrintTemplate {
  /**
   * 执行订单列表打印
   * @param {Array} orderList - 订单列表数据
   * @param {Function} formatDate - 日期格式化函数
   */
  print(orderList, formatDate) {
    // 初始化hiprint，注册默认元素类型提供者
    hiprint.init({
      providers: [new defaultElementTypeProvider()]
    })

    // 创建打印模板实例，定义打印模板结构
    const printTemplate = new hiprint.PrintTemplate({
      template: {
        panels: [
          {
            index: 0,
            name: "订单数据表格",
            height: 297,
            width: 210,
            paperHeader: 0,
            paperFooter: 842,
            printElements: [
              {
                // 标题元素配置
                options: {
                  left: 10,
                  top: 40,
                  height: 30,
                  width: 580,
                  title: "销售订单列表",
                  fontSize: 28,
                  fontWeight: "700",
                  textAlign: "center"
                },
                printElementType: {
                  title: "标题",
                  type: "text"
                }
              },
              {
                // 表格元素配置
                options: {
                  left: 10,
                  top: 80,
                  height: 100,
                  width: 580,
                  field: "table",
                  tableHeaderRepeat: "first",
                  tableFooterRepeat: "last",
                  columns: [
                    [
                      { title: "订单编号", field: "order_number", width: 80, checked: true },
                      { title: "订单日期", field: "order_date", width: 80, checked: true },
                      { title: "客户名称", field: "customer_name", width: 100, checked: true },
                      { title: "产品名称", field: "product_name", width: 100, checked: true },
                      { title: "婴儿名称", field: "baby_name", width: 80, checked: true },
                      { title: "数量", field: "quantity", width: 40, checked: true }
                    ]
                  ],
                  lineHeight: 35,
                  tableHeaderRowHeight: 35  // 设置表头行高为35像素
                },
                printElementType: {
                  title: "订单数据表格",
                  type: "table",
                  editable: true,
                  columnDisplayEditable: true,
                  columnDisplayIndexEditable: true,
                  columnTitleEditable: true,
                  columnResizable: true,
                  columnAlignEditable: true,
                }
              }
            ]
          }
        ]
      }
    })

    // 准备打印数据，将订单列表数据转换为打印所需格式
    const printData = {
      table: orderList.map(order => ({
        order_number: order.order_number || '-',           // 订单编号
        order_date: formatDate(order.order_date) || '-',  // 订单日期（格式化）
        customer_name: (order.customer && order.customer.customer_name) || order.customer_name || '-',  // 客户名称
        product_name: (order.product && order.product.product_name) || order.product_name || '-',      // 产品名称
        baby_name: order.baby_name || '-',  //婴儿名称
        quantity: order.quantity || 0,                  // 数量
      }))
    }

    // 执行打印预览
    printTemplate.print(printData)
  }
}

export default OrderListPrintTemplate
